/**
 * Created by hjqi on 2016/2/12.
 */

var httpRequest = require('../common/httpRequest').httpRequest;
var _req = new httpRequest();
var logger = require('../log4js/log4js').logger('git');
var RETRYTIMES = 3;
var RETRYTIMEOUT = 1000;

module.exports = gitBase;

function gitBase(){
    this._getUserUrl = this._getUserUrl || {};
    this._authorizationHeader  = this._authorizationHeader || { 'Authorization':'token '};
}

gitBase.prototype.getUserInfo = function(token,cb){

    var auth_header = this._initReqHeader(token);
    var me = this;
    var _retryTime = RETRYTIMES;

    var _callfunc = function(){
        _req._request(me._getUserUrl,'GET',auth_header,'',function(data,result,response){
            var _ret = {};
            if(data !== null && data.data !== undefined){
                logger.debug("get user info from git:"+data.data);
                _ret = me._parseUserInfo(data.data);
                _ret.token = token;
                cb(_ret);
            }else{
                if(result === undefined && data instanceof Error){
                    if(data.message.indexOf('ENETUNREACH') !== -1 || data.message.indexOf('ENOTFOUND') !== -1){
                        if(_retryTime > 0){
                            logger.debug("need to recall due to " + data.message);
                            setTimeout(_callfunc,RETRYTIMEOUT);
                            _retryTime--;
                        }else{
                            logger.debug("recall execeed retry times");
                            cb(null,{
                                "code":400,
                                "msg":"get user repo info error"
                            });
                        }
                    }else{
                        cb(null,{
                            "code":400,
                            "msg":data.message
                        });
                    }
                }else
                    cb(null,me._processErr(result));
            }

        });
    }
    _callfunc();

}

gitBase.prototype.getRepoInfo = function(userinfo,cb){

    var auth_header = this._initReqHeader(userinfo.token);
    var me = this;
    var _retryTime = RETRYTIMES;
    var _callfunc = function() {
        _req._request(userinfo.repoUrl, 'GET', auth_header, '', function (data, result, response) {
            var _ret = {};
            if (data !== null && data.data !== undefined) {
                logger.debug("get repo info from git:" + data.data);
                _ret = me._parseRepoInfo(data.data);
                cb(_ret);
            } else {
                if (result === undefined && data instanceof Error) {
                    if(data.message.indexOf('ENETUNREACH') !== -1 || data.message.indexOf('ENOTFOUND') !== -1){
                        if(_retryTime > 0) {
                            logger.debug("need to recall due to " + data.message);
                            setTimeout(_callfunc, RETRYTIMEOUT);
                            _retryTime--;
                        }else{
                            logger.debug("recall execeed retry times");
                            cb(null,{
                                "code":400,
                                "msg":"get repo info error"
                            });
                        }
                    }else{
                        cb(null,{
                            "code":400,
                            "msg":data.message
                        });
                    }
                } else
                    cb(null, me._processErr(result));
            }

        });
    }
    _callfunc();
}

gitBase.prototype.getTagInfo = function(full_name,userinfo,cb){
    var auth_header = this._initReqHeader(userinfo.token);
    var _tags_url = this._getTagUrl(full_name);
    var me = this;
    var _retryTime = RETRYTIMES;
    var _callfunc = function() {
        _req._request(_tags_url, 'GET', auth_header, '', function (data, result, response) {
            var taglst = [];
            if (data !== null && data.data !== undefined) {
                logger.debug("get repo info from git:" + data.data);
                taglst = me._parseTagInfo(data.data);
                cb(taglst.sort());
            } else {
                if (result === undefined && data instanceof Error) {
                    if(data.message.indexOf('ENETUNREACH') !== -1 || data.message.indexOf('ENOTFOUND') !== -1){
                        if(_retryTime > 0) {
                            logger.debug("need to recall due to " + data.message);
                            setTimeout(_callfunc, RETRYTIMEOUT);
                            _retryTime--;
                        }else{
                            logger.debug("recall execeed retry times");
                            cb(null,{
                                "code":400,
                                "msg":"get tag info error"
                            });
                        }
                    }else{
                        cb(null,{
                            "code":400,
                            "msg":data.message
                        });
                    }
                } else
                    cb(null, me._processErr(result));
            }
        });
    }
    _callfunc();
}

gitBase.prototype._initReqHeader = function(token){
    var auth_header = {};
    auth_header.Authorization = this._authorizationHeader.Authorization;
    auth_header.Authorization += token;
    auth_header["User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.101 Safari/537.36";
    return auth_header;
}

gitBase.prototype._parseUserInfo = function(userInfo){}

gitBase.prototype._parseRepoInfo = function(repoInfo){
    var _infoObjlst = JSON.parse(repoInfo);
    var _retlst = [];
    var me = this;
    _infoObjlst.forEach(function(one){
        _retlst.push(me._parseRepoItem(one));
    });
    return _retlst;
}

gitBase.prototype._parseTagInfo = function(tagInfo){}

gitBase.prototype._getTagUrl = function(full_name){}

gitBase.prototype._parseRepoItem = function(repoItem){}

gitBase.prototype._processErr = function(result){}